package cn.tedu.lbs.basic.web.service;

import cn.tedu.lbs.basic.web.repository.AdminRepository;
import cn.tedu.lbs.basic.web.utils.AuthenticationContextUtil;
import com.tarena.lbs.base.common.utils.Asserts;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.common.passport.principle.UserPrinciple;
import com.tarena.lbs.pojo.basic.po.AdminPO;
import com.tarena.lbs.pojo.basic.vo.AdminVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AdminService {
    @Autowired
    private AdminRepository adminRepository;
    public AdminVO adminDetail() throws BusinessException {
        //作为过滤逻辑的下游代码 可以从同一个线程对象 获取threadLocal存储的数据
        UserPrinciple userPrinciple = AuthenticationContextUtil.get();
        //添加一个严谨的 断言
        Asserts.isTrue(userPrinciple==null,new BusinessException("-2","用户认证解析失败"));
        Integer id=userPrinciple.getId();
        //po 和 vo几乎 所有都是属性相同的,vo只会比po少属性 不会多属性
        //1.getById
        AdminPO po = adminRepository.getById(id);
        //2.转化成vo
        AdminVO vo = new AdminVO();
        BeanUtils.copyProperties(po, vo);
        return vo;
    }
}
